<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/



class edit_worker  {

	var $title = FALSE;
	var $name = 'edit_worker';
	var $entry = FALSE;

	var $id = FALSE;

	var $username = FALSE;
	var $timesheet_file = FALSE;
	var $timesheet_file_btn = FALSE;
	
	var $db_file;

	function __construct($title="Edit_Customer",$name=FALSE) {
		$this->title = $title;	
		if ( $name ) $this->name = $name;
	}


	function edit($id,$db_file) {

		// $id == FALSE add new record

		$this->id = $id;
		$this->db_file = $db_file;
		
		$cnf = new conf_timesheet(INIFILE);

		$glade = new GladeXML(PATH_RES.'edit_worker.glade','edit_worker');
		$glade->signal_autoconnect_instance($this);

		$dialog = $glade->get_widget('edit_worker');
		$dialog->set_title($this->title);


/*
		$labels = array('name_lbl','company_lbl','email_lbl');
		foreach ( $labels as $lbl ) {
			$w = $glade->get_widget($lbl);
			$w->set_label(settings::xlate($w->get_label()));
		}		
*/


		$this->username = $glade->get_widget('username');
		$f = $glade->get_widget('first_name');
		$l = $glade->get_widget('last_name');
		$e = $glade->get_widget('email');
		$this->timesheet_file = $glade->get_widget('timesheet_file');
		$this->timesheet_file_btn = $glade->get_widget('timesheet_file_btn');

		// Don't allow to enter a timesheet file for an Add as we don't have a worker id yet.
		if ( ! $id ) {
			$this->timesheet_file->set_sensitive(FALSE);
			$this->timesheet_file_btn->set_sensitive(FALSE);
		}

		$username = '';
		$first_name = '';
		$last_name = '';
		$email = '';
		$timesheet_file = '';

		// Edit an existing customer record
		if ( $this->id ) {
			$wrk = new Workers($this->db_file);
			$username = $wrk->getField($this->id,'username');
			$first_name = $wrk->getField($this->id,'first_name');
			$last_name = $wrk->getField($this->id,'last_name');
			$email = $wrk->getField($this->id,'email');
			$timesheet_file = $wrk->getField($this->id,'timesheet_file');
			$wrk->close();
			unset($wrk);
		}

			
		$this->username->set_text($username);
		$f->set_text($first_name);
		$l->set_text($last_name);
		$e->set_text($email);
		$this->timesheet_file->set_text($timesheet_file);

        $buttons = $dialog->action_area->get_children();
        $button_ok = $buttons[1]; // get the ID of the OK button

        // simulate button click when user press enter
/*
		$this->entry->set_text($default);
        $this->entry->connect('activate', array(&$this, 'on_enter'), $button_ok);
*/



        $dialog->show_all();

		$ok = FALSE;
		while ( ! $ok ) {

        $result = $dialog->run();

		//echo "+ ".Gtk::RESPONSE_OK."\n";
		//echo "- ".Gtk::RESPONSE_CANCEL."\n";
		//echo "->[$result]\n";

		if ( $result == Gtk::RESPONSE_OK ) {

			$username = $this->username->get_text();
			$first_name = $f->get_text();
			$last_name = $l->get_text();
			$email = $e->get_text();
			$timesheet_file = trim($this->timesheet_file->get_text());


			if ( $this->id && $timesheet_file <> '' ) {
				$ok = $this->validate($timesheet_file);
			}
			else {
				$ok = TRUE;
			}
			
			if ( $ok ) {
					
				$wrk = new Workers($this->db_file);

				if ( $this->id ) {
					// Existing record
					$wrk->updateField($this->id,'username',$username);							
					$wrk->updateField($this->id,'first_name',$first_name);							
					$wrk->updateField($this->id,'last_name',$last_name);							
					$wrk->updateField($this->id,'email',$email);							
					$wrk->updateField($this->id,'timesheet_file',$timesheet_file);							

				}
				else {
					// New record
					$id = $wrk->addWorker($username, $first_name, $last_name, $email, $timesheet_file);
				}
						
				$wrk->close();
				unset( $wrk );
			}


			$result = $id;

		}
		else {
			$ok = TRUE;
			$result = FALSE;		// cancel
		}

		}

        $dialog->destroy();

		return($result);

    }


	// simulate button click when user press enter
    function on_enter($entry, $button) {
        $button->clicked();
    }


	/**
	*	Select a worker timesheet file
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onSelectFile() {

		$username = $this->username->get_text();
		$timesheet_file = $this->timesheet_file->get_text();

		$last_path = dirname($timesheet_file);
		$last_file = basename($timesheet_file);

		if ( $last_path == '' ) $last_path = "C:";			

		$filefilter = array(
							"Timesheet Data"		=> "*.db",
							"All files"			=> "*.*"
							);
	

		$filename = platform::file_select($last_path,$last_file,$filefilter);
		if ( ! $filename ) return;
		
		
		if ( $this->validate($filename) ) $this->timesheet_file->set_text($filename);

	}

	/**
	*	Validate the timesheet file
	*	
	*	@param	
	*	@return	
	*	
	*/
	function validate($filename) {

		// Exists ?
		if ( ! file_exists($filename) ) {
			platform::error_popup("File does not exist :".$filename,'Oops!');								
			return(FALSE);
		}
		
		
		// Is this a timesheet file ??

		$ts = new Timesheet($filename);
		$db_ver = $ts->db_ver;
		$ts->close();
		unset($ts);

		if ( ! $db_ver ) {
			platform::error_popup("Does not appear to be timesheet data :".$filename,'Error!');								
			return(FALSE);
		}

		if ( $db_ver < 1.5 ) {
			platform::error_popup("Timesheet database version is $db_ver. It needs to be >= 1.5",'Error!');								
			return(FALSE);
		}
									
		// Is it already in use by another worker ??
		$wrk = new Workers($this->db_file);
		$used = $wrk->isUsed($this->id,$filename);
		$wrk->close();
		unset($wrk);

		if ( $used ) {
			platform::error_popup("This timesheet is already being used.",'Error!');								
			return(FALSE);
		}

		// Is it a timesheet file for this worker?
		$ts = new Timesheet($filename);
		$db_ver = $ts->db_ver;
		$managed = $ts->isManaged();
		$worker = $ts->getWorker();

		if ( $managed ) {
			if ( $worker <> $this->id ) {
				platform::error_popup("Timesheet is already used by another worker.",'Error!');								
				$ts->close();
				unset($ts);
				return(FALSE);
			}
		}
		else {
			// Not an already managed timesheet - make it managed by this worker.
			$ts->setWorkerId($this->id);
		}

		$ts->close();
		unset($ts);
		
		return(TRUE);

	}



}

?>